%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Trapping with Zeeman structure F=1/2 and radiofrequency.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;											Constant;

B0=.0001e-4;																								%offset in G
dB=200e-2;																									%gradient in G/cm
z =[0:.0005:.55]*1e-6;																					%z-axis
gF=   2/3;
B =gF*uB/(2*pi*hP)*sqrt((dB*z).^2+B0^2);												    			%B-field

d =  5e3;									ld=length(d);											%microwave frequency
o =  700;																			    						%Rabi frequency

for k=1:length(z)
	[dum,ind]=min(abs(B(k)-d));			dd(k)=d(ind);

%	L(k)=.5*sqrt(o^2+(B(k)-d)^2);
	L(k)=.5*(B(k)-dd(k)) + o^2./(B(k)-d)/4;

	H=[	.5*B(k)-.5*dd(k)	o/2
		o/2					-.5*B(k)+.5*dd(k)	];

	D(:,k)=(eig(H));
end
[dum,ind]=min(abs(B'*(d./d)-(B'./B')*d));

figure(1);										clf;
subplot(221),plot(z*1e6,.5*B*1e-3,'k',z*1e6,-.5*B*1e-3,'k','LineWidth',1.4);
hold on;
plot([z(ind) z(ind)]*1e6,[-.9 .9]*B(ind)*.5e-3,'k','LineWidth',3);
fill([z(ind) z(ind)+1.5e-8 z(ind)-1.5e-8]*1e6, [1 .8 .8]*B(ind)*.5e-3,'k','EdgeColor','k');
fill([z(ind) z(ind)+1.5e-8 z(ind)-1.5e-8]*1e6,-[1 .8 .8]*B(ind)*.5e-3,'k','EdgeColor','k');
hold off;
set(gca,'xLim',[0 .45],'yLim',[-4 4],'Linewidth',1.4,'FontSize',16);
xlabel('\fontsize{16}{\itz}   (\mum)');
ylabel('\fontsize{16}\Delta/2{\it\pi}   (kHz)');
text(.02,3.5,'\fontsize{16}(a)')
text(.28,0,'\fontsize{16}\omega')
subplot(222),plot(z*1e6,.5*[-1;1]*(B-d)*1e-3,':k',z*1e6,D(1,:)*1e-3,'k',z*1e6,D(2,:)*1e-3,'k','LineWidth',1.4);
hold on;
plot(z(z<z(ind))*1e6,L(z<z(ind))*1e-3,'-.k',z(z>z(ind))*1e6,L(z>z(ind))*1e-3,'-.k','LineWidth',1.4);
plot([z(ind) z(ind)]*1e6,[-.9 .9]*o*.5e-3,'k','LineWidth',3);
fill([z(ind) z(ind)+.8e-8 z(ind)-.8e-8]*1e6, [1 .7 .7]*o*.5e-3,'k','EdgeColor','k');
fill([z(ind) z(ind)+.8e-8 z(ind)-.8e-8]*1e6,-[1 .7 .7]*o*.5e-3,'k','EdgeColor','k');
set(gca,'xLim',[.15 .4],'yLim',[-1.5 1.5],'Linewidth',1.4,'FontSize',16);
xlabel('\fontsize{16}{\itz}   (\mum)');
ylabel('\fontsize{16}\Delta/2{\it\pi}   (kHz)');
text(.16,1.3,'\fontsize{16}(b)')
text(.28,0,'\fontsize{16}\Omega')
text(.32,0,'\fontsize{16}\Delta')
